package j2EE.tag;

import com.fasterxml.jackson.databind.DatabindContext;
import j2EE.bean.RSSInfo;
import j2EE.bean.impl.RSSInfoImpl;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import javax.servlet.jsp.tagext.Tag;
import java.awt.font.TextHitInfo;

/**
 * Created with Intel IDEA.
 *
 * @Auther: gjt
 * @Date: 2024/9/8 15:21
 */
public class RSSChannelTag extends SimpleTagSupport {
    // 变量名
    private static final String NAME_ATTR = "channelName";
    private static final String LINK_ATTR = "channelLink";

    // 输入参数
    private String url;
    // RSS解析器
    private RSSInfo rssInfo;

    public RSSChannelTag(){
        this.rssInfo = new RSSInfoImpl();
    }

    // 以来自标签的url 参数调用
    public void setUrl(String url){
        this.url = url;
    }

    // 由RSSItemsTag使用
    protected RSSInfo getRssInfo(){
        return rssInfo;
    }

    // 解析RSS并建立脚本变量
    public int doStartTag() throws JspTagException{
        try {
            rssInfo.parse(url);

            DatabindContext pageContext = null;
            pageContext.setAttribute(NAME_ATTR, rssInfo.getChannelTitle());
            pageContext.setAttribute(LINK_ATTR, rssInfo.getChannelLink());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Tag.EVAL_BODY_INCLUDE;
    }
}
